package dolf.window;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.util.List;
import dolf.game.*;
import dolf.objects.Hole;
import dolf.objects.WorldObject;
import dolf.objects.GroundObject;
import dolf.objects.BloodObject;

public class Canvas extends JComponent implements MouseListener {
	private static final long serialVersionUID = 1L;
	private static Canvas instance = new Canvas();
	private boolean antialiasing;
	private boolean dithering;
	private int interpolation;
	
	private Canvas(){
		//this.clearList();
		this.setFocusable(true);
		this.setPreferredSize(new Dimension(570,500));
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseMoved(MouseEvent evnt){
				Level.getInstance().setMouse(evnt.getX(), evnt.getY());
			}
		});
		this.addMouseListener(this);
	}
	public static Canvas getInstance() {
		return instance;
	}
	
	public void paint(Graphics g){
		Graphics2D gr = (Graphics2D)g;
		List<WorldObject> objects = Level.getInstance().getObjects();
		List<GroundObject> ground = Level.getInstance().getGrounds();
		List<BloodObject> blood = Level.getInstance().getBlood();
		List<Hole> holes = Level.getInstance().getHoles();
		List<WorldObject> animations = Level.getInstance().getAnimations(); 
		//hintergrundbild
		//java.awt.Image pic = java.awt.Toolkit.getDefaultToolkit().getImage("gfx/about.png");
		//gr.drawImage(pic,0,0,650, 500, null);
		
		
		if (antialiasing) gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			else gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		
		if (dithering) gr.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
			else gr.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
		
		if (interpolation == 0) gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
		else if (interpolation == 1) gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		else if (interpolation == 2) gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		
		Level.getInstance().paint(gr);
		
		for(int i = 0; i < ground.size(); i++){
			if(!(ground.get(i) instanceof WorldObject)){
				continue;
			}
			WorldObject obj =(WorldObject) ground.get(i);
			if(obj.isVisible()){
				obj.paint(gr);
			}
		}
		
		BloodManager.getInstance().drawDryBlood(gr);
		
		for(int i = 0; i < holes.size(); i++){
			holes.get(i).paint(gr);
		}
		
		
		for(int i = 0; i < objects.size(); i++){
			WorldObject obj = objects.get(i);
			if(obj.isVisible()){
				obj.paint(gr);
			}
		}
		for(int i = 0; i < animations.size(); i++){
			animations.get(i).paint(gr);
		}
		for(int i = 0; i < blood.size(); i++){
			blood.get(i).paint(gr);
		}
	}
	public void update(Graphics g){}
	
	public void mousePressed(MouseEvent e) {
		Level.getInstance().setPressed(true);
    }

     public void mouseReleased(MouseEvent e) {
    	 Level.getInstance().setPressed(false);
     }

     public void mouseEntered(MouseEvent e) {}

     public void mouseExited(MouseEvent e) {}

     public void mouseClicked(MouseEvent e) {
    	 Level.getInstance().setPressed(false);
    	 Level.getInstance().setClicke(true);
     }
     
     public void setAntialiasing(boolean value) {
    	 antialiasing = value;
     }
     public boolean getAntialiasing() {
    	 return antialiasing;
     }
     
     public void setDithering(boolean value) {
    	 dithering = value;
     }
     public boolean getDithering() {
    	 return dithering;
     }
     
     public void setInterpolation(int value) {
    	 interpolation= value;
     }
     public int getInterpolation() {
    	 return interpolation;
     }
     
}
